package cn.edu.zhku.citrusmonitor.baseinfo.citrus.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import cn.edu.zhku.citrusmonitor.baseinfo.citrus.exception.CitrusException;
import cn.edu.zhku.citrusmonitor.baseinfo.citrus.service.CitrusService;
import cn.edu.zhku.citrusmonitor.baseinfo.citrus.service.CitrusServiceImpl;
import cn.edu.zhku.citrusmonitor.constant.Constant;
import cn.edu.zhku.citrusmonitor.domain.Bean;
import cn.edu.zhku.citrusmonitor.domain.TbCitrus;
import cn.edu.zhku.citrusmonitor.util.BaseServlet;
import cn.edu.zhku.citrusmonitor.util.FileOperationUtil;
import cn.edu.zhku.citrusmonitor.util.Message;
import cn.edu.zhku.citrusmonitor.util.PageBean;
import cn.edu.zhku.citrusmonitor.util.PrintMessageUtil;

@WebServlet("/citrus.do")
public class CitrusServlet extends BaseServlet {
	
	private static final long serialVersionUID = 1L;
	
	// 添加
	public String addCitrus(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		TbCitrus citrus = new TbCitrus();
		try {
			// 处理上传文件表单
			Map<String, String[]> map = FileOperationUtil.upload(request, Constant.CITRUS_IMG_PATH);		
			BeanUtils.populate(citrus, map);
			CitrusService citrusService = new CitrusServiceImpl();
			// 添加信息
			citrus.setCitrusId(UUID.randomUUID().toString());
			boolean res = citrusService.addCitrus(citrus);
			return PrintMessageUtil.printMessage(response, res);	// 返回结果
		} catch (CitrusException e) {
			e.printStackTrace();
			return JSON.toJSONString(Message.errMsg(e.getMessage()));
		} catch (Exception e) {
			e.printStackTrace();
			return JSON.toJSONString(Message.errMsg("服务器繁忙!"));
		}
	}
	
	// 删除单个
	public String removeCitrus(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String citrusId = request.getParameter("citrusId");
		CitrusService citrusService = new CitrusServiceImpl();
		try {
			boolean res = citrusService.removeCitrus(citrusId);
			return PrintMessageUtil.printMessage(response, res);	// 返回结果
		} catch (CitrusException e) {
			e.printStackTrace();
			return JSON.toJSONString(Message.errMsg(e.getMessage()));
		}
	}
	// 批量删除
	public String removeCitruses(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String param = request.getParameter("ids");
		String[] ids = param.split(",");
		CitrusService citrusService = new CitrusServiceImpl();
		boolean result = false;
		try {
			result = citrusService.removeCitrusesByIds(ids);
			return PrintMessageUtil.printMessage(response, result);	
		} catch (CitrusException e) {
			e.printStackTrace();
			return JSON.toJSONString(Message.errMsg(e.getMessage()));
		}
	}
	
	// 修改
	public String modifyCitrus(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		TbCitrus citrus = new TbCitrus();
		try {
			// 封装数据
			BeanUtils.populate(citrus, request.getParameterMap());
			CitrusService citrusService = new CitrusServiceImpl();
			// 修改信息
			boolean res = citrusService.modifyCitrus(citrus);
			return PrintMessageUtil.printMessage(response, res);	// 返回结果
		} catch (CitrusException e) {
			e.printStackTrace();
			return JSON.toJSONString(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			return JSON.toJSONString(Message.errMsg("服务器繁忙!"));
		}
	}
	
	// 根据id查询，返回TbCitrus类
	public String findCitrusById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String citrusId = request.getParameter("citrusId");
		CitrusService citrusService = new CitrusServiceImpl();
		TbCitrus citrus = null;
		try {
			citrus = citrusService.findCitrusById(citrusId);
			return PrintMessageUtil.printMessageWithDateFormat(response, citrus, "yyyy-MM-dd HH:mm:ss");	// 返回结果
		} catch(CitrusException e) {
			e.printStackTrace();
			return JSON.toJSONString(Message.errMsg(e.getMessage()));
		}
	}
	
	// 根据id查询，返回Bean信息类
	public String getCitrusById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String citrusId = request.getParameter("id");
		CitrusService citrusService = new CitrusServiceImpl();
		Bean bean = null;
		try {
			bean = citrusService.getCitrusById(citrusId);
			return JSON.toJSONStringWithDateFormat(bean, "yyyy-MM-dd HH:mm:ss", SerializerFeature.EMPTY);
		} catch(CitrusException e) {
			e.printStackTrace();
			return JSON.toJSONString(Message.errMsg(e.getMessage()));
		}
	}

	// 分页查询所有
	public String findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			return fySearch(request, response, null);
		} catch (CitrusException e) {
			e.printStackTrace();
			return JSON.toJSONString(Message.errMsg(e.getMessage()));
		}
	}

	// 根据名称查询，模糊查询
	public String findCitrusByNameLike(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String citrusName = request.getParameter("citrusName");
		try {
			return fySearch(request, response, citrusName);
		} catch (NumberFormatException e) {
			e.printStackTrace();
			return JSON.toJSONString(Message.errMsg("请求参数有误!"));
		} catch (CitrusException e) {
			e.printStackTrace();
			return JSON.toJSONString(Message.errMsg(e.getMessage()));
		}
	}
	
	// 设置柑橘推荐技术
	public String setCitrusTnlg(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String citrusId = request.getParameter("citrus_id");
		String technologyId = request.getParameter("technology_id");
		CitrusService citrusService = new CitrusServiceImpl();
		try {
			boolean res = citrusService.setCitrusTnlg(citrusId, technologyId);
			return PrintMessageUtil.printMessage(response, res);
		} catch (CitrusException e) {
			e.printStackTrace();
			return JSON.toJSONString(Message.errMsg(e.getMessage()));
		}
	}
	// 根据要求返回时间靠前的size条数据，返回字段包含: 柑橘主键，柑橘名，成长周期，柑橘图片路径， 时间
	public String getCitruses(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		CitrusService citrusService = new CitrusServiceImpl();
		String size = request.getParameter("pageSize");
		if(size == null) {
			size = Constant.DEFAULT_PAGESIZE;
		}
		List<TbCitrus> cList = null;
		try {
			cList = citrusService.findCitrusesLimit(Integer.parseInt(size));
			return PrintMessageUtil.printListMessageWithDateFormat(response, cList, Constant.DEFAULT_DATEFORMAT);
		} catch (NumberFormatException e) {
			e.printStackTrace();
			return JSON.toJSONString(Message.errMsg("请求参数有误!"));
		} catch (CitrusException e) {
			e.printStackTrace();
			return JSON.toJSONString(Message.errMsg(e.getMessage()));
		}
	}
	
	// 提供前台分页展示数据
	public String getCitrusPageList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String pageSize = request.getParameter("pageSize");
		if(pageSize==null) {
			pageSize = Constant.DEFAULT_PAGESIZE;
		}
		String currentPage = request.getParameter("currentPage");
		CitrusService citrusService = new CitrusServiceImpl();
		PageBean<Bean> pageBean = null;
		try {
			pageBean = citrusService.findPageList(Integer.parseInt(pageSize), Integer.parseInt(currentPage));
			return JSON.toJSONStringWithDateFormat(pageBean, Constant.DEFAULT_DATEFORMAT, SerializerFeature.EMPTY);
		} catch (NumberFormatException e) {
			e.printStackTrace();
			return JSON.toJSONString(Message.errMsg("请求参数有误!"));
		} catch (CitrusException e) {
			e.printStackTrace();
			return JSON.toJSONString(Message.errMsg(e.getMessage()));
		}
	}
	
	// 分页查询
	private String fySearch(HttpServletRequest request, HttpServletResponse response, Object condition) throws IOException, NumberFormatException, CitrusException {
		String pageSize = request.getParameter("pageSize");
		if(pageSize==null) {
			pageSize = Constant.DEFAULT_PAGESIZE;
		}
		String currentPage = request.getParameter("currentPage");
		CitrusService citrusService = new CitrusServiceImpl();
		PageBean<TbCitrus> pageBean = null;
		if(condition != null) {
			pageBean = citrusService.findCitrusByNameLike(Integer.parseInt(pageSize), Integer.parseInt(currentPage), (String)condition);
		}else{
			pageBean = citrusService.fySearchAll(Integer.parseInt(pageSize), Integer.parseInt(currentPage));
		}
		return JSON.toJSONStringWithDateFormat(pageBean, Constant.DEFAULT_DATEFORMAT, SerializerFeature.EMPTY);
	}
	
	
}
